package cn.wowjoy.pda.android_pharmacy.ui.activity.staticH.put;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import java.util.List;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.common.core.BaseActivity;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.databinding.ActivityStaticPutIndexBinding;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.putInfo.PutTotalBeen;
import cn.wowjoy.pda.android_pharmacy.mvvm.presenter.staticH.StaticPutIndexPresenter;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.staticH.StaticPutIndexViewModel;
import gear.yc.com.gearlibrary.rxjava.rxbus.RxBus;
import io.reactivex.functions.Consumer;

public class StaticPutIndexActivity extends BaseActivity<ActivityStaticPutIndexBinding,StaticPutIndexViewModel,StaticPutIndexPresenter> {
    //PDA扫码广播
    private ScanReceiver mScanReceiver;
    private IntentFilter intentFilter;

    @Override
    protected void init(Bundle savedInstanceState) {
        binding.setViewModel(viewModel);
        binding.setPresenter(presenter);
        binding.tvPutIndexTitle.titleTextTv.setText("药品上架");
        binding.tvPutIndexTitle.titleBackTv.setText("");
        binding.tvPutIndexTitle.titleBackLl.setVisibility(View.VISIBLE);
        binding.tvPutIndexTitle.titleBackLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mScanReceiver = new ScanReceiver();
        intentFilter = new IntentFilter();
        intentFilter.addAction(Contants.WOW_NEW_ADD);
        binding.rvDoorPutIndex.setLayoutManager(new LinearLayoutManager(this));
        binding.rvDoorPutIndex.setPullRefreshEnabled(false);
        RxBus.getInstance().tObservable(Contants.STATIC_PUT_INDEX_PENDING_COUNT, List.class).subscribe(new Consumer<List<PutTotalBeen>>() {
            @Override
            public void accept(List<PutTotalBeen> putTotalBeens) throws Exception {
                if(null == putTotalBeens.get(0).getDJJLID()|| putTotalBeens.get(0).getDJJLID().equals("") ){
                    binding.tvPutIndexPendingNumber.setText("0");
                    return;
                }
                binding.tvPutIndexPendingNumber.setText("" + putTotalBeens.size());
            }
        });
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_static_put_index;
    }

    @Override
    protected Class<StaticPutIndexPresenter> getPresenterClass() {
        return StaticPutIndexPresenter.class;
    }

    @Override
    protected Class<StaticPutIndexViewModel> getViewModelClass() {
        return StaticPutIndexViewModel.class;
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.getDataList();
        registerReceiver(mScanReceiver, intentFilter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(mScanReceiver);
    }

    //TODO:接收PDA扫码广播，准备跳转
    class ScanReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Contants.WOW_NEW_ADD)) {
//                Log.e("TAG", "onReceive: " + intent.getStringExtra(Contants.WOW_NEW_KEY));
                //要做下扫码的过滤判断
                String stringExtra = intent.getStringExtra(Contants.WOW_NEW_KEY);
                //TODO:跳转传递数据
                Intent mIntent = new Intent(StaticPutIndexActivity.this, StaticPutMedicineDetailActivity.class);
                mIntent.putExtra("DJJLID", stringExtra);
                startActivity(mIntent);
            }
        }
    }
}
